import {Component, OnInit, ViewContainerRef} from '@angular/core';
import {Prodoutbound} from "../../../../model/Prodoutbound";
import {NzModalService} from "ng-zorro-antd/modal";
import {AdminService} from "../../../../services/admin.service";
import {AlertService} from "../../../../services/alert.service";
import {CpckeditComponent} from "../cpckedit/cpckedit.component";
import {Kcsl} from "../../../../model/Kcsl";

@Component({
  selector: 'app-cpcklist',
  templateUrl: './cpcklist.component.html',
  styleUrls: ['./cpcklist.component.css']
})
export class CpcklistComponent implements OnInit {
  pl:Array<Prodoutbound>
  kl:Array<Kcsl>
  constructor(private modal: NzModalService, private viewContainerRef: ViewContainerRef,
              private adminService: AdminService,
              private alertService: AlertService) {
    this.pl = new Array();
    this.kl = new Array<Kcsl>();
  }

  ngOnInit(): void {
    this.getProdoutbounds();
    this.getKcsl();
  }

  getProdoutbounds(){
    this.adminService.getProdoutbounds().then((data:any)=>{
      if(data.msg == "ok"){
        this.pl = data.list
      }else{
        this.alertService.showError("获取出库信息列表失败")
      }
    })
  }

  add(){
    const modalwindow = this.modal.create({
      nzTitle: '出库信息编辑',
      nzContent: CpckeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {

        optype:"add"
      },

      nzOnOk: () => new Promise(resolve => {
        modalwindow.getContentComponent().save();
        setTimeout(() => {

          resolve(true);
          this.getProdoutbounds()
          this.getKcsl();

        }, 2000)
      })
    })
  }
  update(id:string){
    const modalwindow = this.modal.create({
      nzTitle: '出库信息编辑',
      nzContent: CpckeditComponent,
      nzViewContainerRef: this.viewContainerRef,
      nzComponentParams: {
        id:id,
        optype:"update"
      },

      nzOnOk: () => new Promise(resolve => {
        modalwindow.getContentComponent().save();
        setTimeout(() => {

          resolve(true);
          this.getProdoutbounds()
          this.getKcsl();

        }, 2000)
      })
    })
  }
  delete(id:string){
    this.adminService.deleteProdoutboundByid(id).then((data:any)=>{
      if(data.msg == "ok"){
        this.alertService.showSuccess("删除成功")
        this.getProdoutbounds()
        this.getKcsl();

      }else{
        this.alertService.showError("删除失败")
      }
    })
  }
  getKcsl(){
    this.adminService.getKcsls().then((data:any)=>{
      if(data.msg == "ok"){
        this.kl = data.list
      }else{
        this.alertService.showError("获取库存数量统计失败")
      }

    })
  }
}
